﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;
using System.Drawing;
using DraggableControls;
using DialogsEditor.DAO;

namespace DialogsEditor
{
    public class DialogEntity : Control
    {
        private int ID;
        public Label title = new Label();
        public int idTrig;
        public Label trigger = new Label();
        public Label charac = new Label();
        public Label text = new Label();
        public int idNextDialog;
        private string titleNxtDlg;
        public string TitleNextDialog
        {
            get { return titleNxtDlg; }
            set
            {
                titleNxtDlg = value;
                /*using(MySqlConnection conn = ConnFactory.getInstancia().getConexao()){
                    if (conn == null) return;
                    idNextDialog = UtilsDAO.GetId(conn, "select id from dialog WHERE", "title", titleNxtDlg);
                }*/
            }
        }
        public bool IsProject { get; set; }
        public int idProject;

        public Point Position {get; set;}

        public bool IsSelected { get; set; }
        public Rectangle Area { get; set; }

        private Label titleAppearingInDialogs = new Label();

        /// <summary>
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="title"></param>
        /// <param name="charac"></param>
        /// <param name="tri"></param>
        /// <param name="txt"></param>
        /// <param name="nextDialog"></param>
        /// <param name="isProj"> 1 == true, 0 == false</param>
        /// <param name="idProj"></param>
        public DialogEntity(int ID, string title, string charac, string tri, string txt, int nextDialog = -1, bool isProj = false, int idProj = -1, List<DialogEntity> list = null)
        {
            this.ID = ID;
            this.title.Text = title;
            this.trigger.Text = tri;
            this.charac.Text = charac;
            this.text.Text = txt;
            this.idNextDialog = nextDialog;
            this.IsProject = isProj;
            this.idProject = idProj;

            /*using (MySql.Data.MySqlClient.MySqlConnection conn = ConnFactory.getInstancia().getConexao())
            {
                this.idTrig = UtilsDAO.GetId(conn, "SELECT id from dialogtrigger WHERE", "nameTrigger", this.trigger.Text);
            }*/

            DefaultSets(list);
        }

        public DialogEntity(int ID, string title, string tri, string txt, int nextDialog = -1, bool isProj = false, int idProj = -1, List<DialogEntity> list = null)
        {
            this.ID = ID;
            this.title.Text = title;
            this.trigger.Text = tri;
            this.text.Text = txt;
            this.idNextDialog = nextDialog;
            this.IsProject = isProj;
            this.idProject = idProj;

            DefaultSets(list);
        }

        public DialogEntity()
        {
            CreateControlsInstance();
            this.Draggable(true);
            Random r = new Random();
            int qtd = r.Next(0, 20);
            if (Location.IsEmpty)
                this.Location = new Point(50 + 300 * (qtd % 4), 100 + 50 * (qtd / 4));
            this.Width = 100;
            this.Height = 50;

            this.BackColor = Color.White;

            this.Area = new Rectangle(Left, Top, Width, Height);

            title.Enabled = false;
            trigger.Enabled = false;
            text.Enabled = false;

            titleAppearingInDialogs.Text = title.Text;
            titleAppearingInDialogs.Parent = this;
            titleAppearingInDialogs.Enabled = false;
        }

        private void DefaultSets(List<DialogEntity> list)
        {
            CreateControlsInstance();
            this.Draggable(true);
            Random r = new Random();
            //int qtd = list != null ? list.Count: 0;
            int qtd = r.Next(0, 20);
            if (list != null)
                qtd = list.Count;
            //this.Location = new Point(100 + 100 * ((qtd - 1) % 4), 100 + 50 * ((qtd - 1)/4));//count comecar com 1
            this.Location = new Point(50 + 300 * (qtd % 4), 100 + 50 * (qtd/4));
            this.Width = 100;
            this.Height = 50;

            this.BackColor = Color.White;

            this.Area = new Rectangle(Left, Top, Width, Height);

            title.Enabled = false;
            trigger.Enabled = false;
            text.Enabled = false;

            titleAppearingInDialogs.Text = title.Text;
            titleAppearingInDialogs.Parent = this;
            titleAppearingInDialogs.Enabled = false;
        }

        public void SetDraggable(bool isDrag)
        {
            this.Draggable(isDrag);
        }

        public int GetID() { return this.ID; }

        public void SetID(int i)
        {
            if (i > 0 && this.ID == -1)
            {
                this.ID = i;
            }
        }

        public void SetMouseUpEvent(bool v)
        {
            if (v)
                this.MouseUp += DialogEntity_MouseUp;
            else
                this.MouseUp -= DialogEntity_MouseUp;
        }

        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Position = new Point(Left + Width/2, Top + Height/2);
            this.Area = new Rectangle(Left, Top, Width, Height);

            ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
                IsSelected ? Color.Yellow : Color.Black, 1, ButtonBorderStyle.Solid,
                IsSelected ? Color.Yellow : Color.Black, 1, ButtonBorderStyle.Solid,
                IsSelected ? Color.Yellow : Color.Black, 1, ButtonBorderStyle.Solid,
                IsSelected ? Color.Yellow : Color.Black, 1, ButtonBorderStyle.Solid);

            e.Graphics.DrawString(titleAppearingInDialogs.Text, Form1.DefaultFont, Brushes.Black, Left + 20, Top + 15);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            Utils.UnselectingAllLinks();
            Utils.UnselectingAllDialogs();

            if (Form1.IsLinking && Form1.Selected != null && Form1.Selected != this)
            {
                VerifyNextDialog();
                Form1.ClearCurrentLink();
                Form1.Selected = null;
            }
            else
            {
                IsSelected = true;

                if (Form1.IsLinking)
                {
                    if (idNextDialog > 0)
                        MessageBox.Show("Esse dialogo já possui uma ligação.");
                    else
                    {
                        Form1.CurrentLink = Form1.MyLineShape();
                        Form1.CurrentLink.MouseDown += CurrentLink_MouseDown;
                        Form1.ContainerShape.Shapes.Add(Form1.CurrentLink);
                    }
                }
            }

            Refresh();
        }

        void CurrentLink_MouseDown(object sender, MouseEventArgs e)
        {
            if (Form1.IsLinking)
            {
                Form1.ClearCurrentLink();
                Form1.Selected = null;
            }
        }

        private void DialogEntity_MouseUp(object sender, MouseEventArgs e)
        {
            /*if (Form1.IsLinking)
            {
                VerifyNextDialog();
                Form1.ClearCurrentLink();
            }
            Refresh();*/
        }

        private void VerifyNextDialog()
        {

            /*foreach (DialogEntity d in Form1.CurrentDialogs)
            {
                //Point p = PointToClient(Cursor.Position);
                if (d.IsSelected || !d.ClientRectangle.Contains(PointToClient(Cursor.Position))) continue;

                if (d.NextDialog(d)) break;
            }*/
            if (!Form1.IsLinking || Form1.Selected == null || Form1.Selected == this)
            {
                if (Form1.Selected == this)
                    MessageBox.Show("Não pode fazer ligação com o mesmo diálogo "+this.title.Text);
                return;
            }

            if (GetID() <= 0)
                Form1.Selected.TitleNextDialog = this.title.Text;
            else
                Form1.Selected.idNextDialog = this.GetID();

            Form1.CurrentLinks.Add(new Line(Form1.Selected, this));
        }

        public bool NextDialog(DialogEntity d)
        {
            if (Form1.Selected == null)
            {
                return false;
            }

            if (GetID() <= 0)
                Form1.Selected.TitleNextDialog = d.title.Text;
            else
                Form1.Selected.idNextDialog = d.GetID();

            Form1.CurrentLinks.Add(new Line(Form1.Selected, d));
            return true;
        }

    }
}
